package cn.com.jrj.job;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;

import cn.com.jrj.base.exception.ApplicationException;
import cn.com.jrj.base.util.ConfigUtil;
import cn.com.jrj.base.util.HttpSnatch;
import cn.com.jrj.base.work.VnetAppWork;

public class VnetBaseWork extends VnetAppWork {

	public static final String TEMPLATE_PATH = "templatePath";

	public static final String PUBLISH_PATH = "publishPath";

	public static final String SEND_PATH_HTML = "sendPath";

	@Override
	protected void executeInternal(Map<String, Object> params) throws ApplicationException, InterruptedException {
		// TODO Auto-generated method stub

	}

	public static String getTemplatePath() {
		return ConfigUtil.getInstance().getString(TEMPLATE_PATH);
	}

	public static String getPublishPath() {
		return ConfigUtil.getInstance().getString(PUBLISH_PATH);
	}

	public static String getSendPath() {
		return ConfigUtil.getInstance().getString(SEND_PATH_HTML);
	}

	/**
	 * 判断当前时间是否是交易时间<BR>
	 * 9:30-11:30 或 13:00-15:00
	 * 
	 * @return
	 */
	protected boolean isTradeTime() {
		boolean b = false;
		Calendar c = Calendar.getInstance();
		int m = c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE);
		if ((m >= 9 * 60 + 30 && m <= 11 * 60 + 30) || (m >= 13 * 60 && m <= 15 * 60)) {
			b = true;
		}
		return b;
	}

	/**
	 * 判断当前日期是不是交易日<BR>
	 * 查询接口:<BR>
	 * eg: http://q.jrjimg.cn/?q=cn|td&y=td&i=20101001<BR>
	 * 说明:<BR>
	 * 如果返回json串中包含 20101001 ,说明是交易日
	 * 
	 * @return
	 * @see http://wiki.jrj.com.cn/pages/viewpage.action?pageId=24806647
	 */
	protected boolean isTradeDay() {
		String d = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String url = "http://q.jrjimg.cn/?q=cn|td&y=td&i=" + d;
		boolean b = false;
		String s = null;
		try {
			s = HttpSnatch.getInstance().GetWebContent(url, "GBK", 5000);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (s != null && s.contains(d)) {
			b = true;
		}
		return b;
	}
}
